<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\property\auth;

use app\traits\CategoryRepository;
use app\common\repositories\BaseRepository;
use app\common\dao\property\auth\PropertyAuthRuleDao;

/**
 * Class propertyAuthRuleRepository
 *
 * @mixin PropertyAuthRuleDao
 */
class PropertyAuthRuleRepository extends BaseRepository
{

    use CategoryRepository;

    public function __construct(PropertyAuthRuleDao $dao)
    {
        $this->dao = $dao;
    }

    public function getList(array $where)
    {
        $query = $this->dao->search($where);
        $count = $query->count();
        $list = $query->select()->toArray();
        foreach ($list as $k => &$v) {
            $v['pid'] = $v['pid'] === 0 ? -1 : $v['pid'];
        }
        return compact('list', 'count');
    }

    public function getUserMenus($propertyUserInfo, $isMenu = true)
    {
        if (empty($propertyUserInfo)) {
            return false;
        }
        $where = [];
        if ($propertyUserInfo['is_main'] != 1) { # 如果是主账号 不验证权限
            /** @var PropertyAuthAccessRepository $accessRepository */
            $accessRepository = app()->make(PropertyAuthAccessRepository::class);
            $roleIds = $accessRepository->getSearch([])
                ->where('property_uid', $propertyUserInfo['id'])
                ->column('role_id');

            /** @var PropertyAuthRoleRepository $repository */
            $roleRepository = app()->make(PropertyAuthRoleRepository::class);

            $ruleIds = $roleRepository->getSearch([])
                ->where('property_id', $propertyUserInfo['property_id'])
                ->where('id', 'in', $roleIds)
                ->column('rules');

            $ruleIds = implode(',', $ruleIds);
            $ruleIds = array_filter(explode(',', $ruleIds));
            if (empty($ruleIds)) {
                return false;
            }
            $where['id'] = $ruleIds;
        }
        if ($isMenu) {
            $where['is_menu'] = 1;
        }
        return $this->dao->getMenuList($where);
    }


    /**
     * 获取权限地址
     * @param $ids
     * @return array
     *
     * @author Amos
     * 2024-01-16 20:45
     */
    public function getRules($ids)
    {
        return $this->dao->getColumn([
            'id' => $ids
        ], 'rule_url');
    }

    public function getCascaderData(array $where = [])
    {
        $list = $this->dao->search($where)
            ->field('name,id,pid')
            ->select()->toArray();
        $list = convert_arr_key($list, 'id');
        $res = formatCascaderData($list, 'name');
        return $res;
    }

}